package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.apache.zookeeper.data.Id;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
@PreAuthorize("hasRole('ROLE_ADMIN')")
public class SetmealController {

    @Reference
    private SetmealService setmealService;


    @Autowired
    private JedisPool jedisPool;

    /**
     * 分页条件查询套餐
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByPage")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean) {
        Result result = setmealService.queryPage(queryPageBean);
        return result;
    }


    /**
     * 文件上传
     *
     * @param imgFile
     * @return
     */
    @RequestMapping("/upload")
    private Result upload(@RequestParam("imgFile") MultipartFile imgFile) {
        try {
            //获取原始文件名
            String oldFileName = imgFile.getOriginalFilename();


            //获取文件后缀
            String suffix = oldFileName.substring(oldFileName.lastIndexOf(".") - 1);

            //生成随机文件名,防止文件重复
            String newFileName = UUID.randomUUID().toString().replaceAll("-", "") + suffix;

            //调用七牛云服务器上传图片
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), newFileName);

            //调用redis存储图片信息
//            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,newFileName);


            return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,newFileName);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(MessageConstant.PIC_UPLOAD_FAIL);
        }

    }


    /**
     * 添加套餐
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @RequestMapping("/add")
    private Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        Result result = setmealService.addSetmeal(setmeal, checkgroupIds);



          //添加成功之后,向redis数据库中添加图片名称,用于清理垃圾图片
//        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, setmeal.getImg());



        return result;
    }


    /**
     * 编辑套餐
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @RequestMapping("/edit")
    private Result edit(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        Result result = setmealService.editSetmeal(setmeal, checkgroupIds);
        return result;
    }

    /**
     * 删除套餐
     * @param id
     * @return
     */
    @RequestMapping("/delete/{id}")
    private Result delete(@PathVariable Integer id){

        Result result = setmealService.deleteSetmeal(id);
        return result;
    }
}
